<?php

namespace App\Events;

use App\Message;
use App\Parsedown;
use App\Utils;

class SendMail
{
    /**
     * Create the event listener.
     *
     */
    public function __construct(){
        //
    }

    /**
     * Handle the event.
     *
     * @param  SolveProblem $event
     * @return void
     */
    public function handle(SolveProblem $event)
    {
        $user = $event->user;
        $task = $event->task;

        $child = $task->child;

        if (!$child->isempty()) {
            $content = "恭喜你,正确的解答了task " . $task->id . " ***" . $task->title . "*** ，你的技能树已扩展，以下是解锁的新task列表:\n\n";
            foreach ($child as $new_task) {
                $content .= "* [" . $new_task->title . "](" . action("welController@show", Utils::id2url($new_task->id)) . ")\n";
            }
            $content .= "\n赶紧回到[首页](" . action("projectsController@newhome") . ")学习新的技能点吧。";
        } else {
            $content = "恭喜你,正确的解答了task " . $task->id . " ***" . $task->title . "***，好厉害！\n\n你已经做到了技能树的最末端了，我们正在加紧后续task的审核，同时也欢迎贡献新的task。";
        }

        $parsedown = new Parsedown();
        $content = $parsedown->text($content);

        $messenger = new Message();
        $messenger->sendMail(1, $user->id, $content);
    }
}
